package cn.ihrm.sass.controller;

import cn.common.Result;
import cn.common.ResultCode;
import cn.controller.BaseController;
import cn.ihrm.sass.entity.company.Company;
import cn.ihrm.sass.entity.company.Department;
import cn.ihrm.sass.response.DeptListResult;
import cn.ihrm.sass.service.CompanyService;
import cn.ihrm.sass.service.DepartmentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 部门管理控制器
 * @author Mxb
 * @version 1.0
 * @date 2020/7/1 14:53
 */
// 解决跨域
@CrossOrigin
@RestController
@Api(description = "部门管理", value = "DepartmentController")
@RequestMapping("/department")
public class DepartmentController extends BaseController {

    @Autowired
    private DepartmentService departmentService;
    @Autowired
    private CompanyService companyService;

    /**
     * 查询所有部门
     *
     * @return
     */
    @ApiOperation(value = "查询所有部门")
    @RequestMapping(value = "findAll", method = RequestMethod.GET)
    public Result findAllDepartment() {
        List<Department> list = departmentService.findAllDepartment(companyId);
        // 指定企业id
        //String companyId = "1";
        // 构造返回对象
        Company company = companyService.findById(companyId);

        DeptListResult deptListResult = new DeptListResult(company,list);

        Result result = new Result(ResultCode.SUCCESS);
        result.setData(deptListResult);
        return result;
    }

    /**
     * 根据id查询部门信息
     * @param id
     * @return
     */
    @ApiOperation(value = "根据id查询部门")
    @RequestMapping(value = "/{id}",method = RequestMethod.GET)
    public Result findById(@PathVariable("id") String id){
        Department department = departmentService.findById(id);
        Result result = new Result(ResultCode.SUCCESS);
        result.setData(department);
        return result;
    }

    /**
     * 修改部门信息
     * @param department
     * @return
     */
    @ApiOperation(value = "修改部门信息")
    @RequestMapping(value = "/{id}",method = RequestMethod.PUT)
    public Result update(@PathVariable("id") String id,@RequestBody() Department department){
        department.setId(id);
        departmentService.update(department);
        return new Result(ResultCode.SUCCESS);
    }

    /**
     * 添加部门信息
     * @param department
     * @return
     */
    @ApiOperation(value = "添加部门信息")
    @RequestMapping(value = "/add",method = RequestMethod.POST)
    public Result add(@RequestBody Department department){
        // 设置保存的企业的id
        department.setCompanyId(companyId);

        // 调用service完成保存企业
        departmentService.save(department);
        // 构造返回结果
        return new Result(ResultCode.SUCCESS);
    }

    /**
     * 根据id删除部门
     * @param id
     * @return
     */
    @ApiOperation(value = "根据id删除部门")
    @RequestMapping(value = "/{id}",method = RequestMethod.DELETE)
    public Result deleteById(@PathVariable("id") String id){
        departmentService.deleteById(id);
        return new Result(ResultCode.SUCCESS);
    }
}
